<?php
require_once 'CZ/Tools/WSDLParser.php';
$generatedString = "";
function getParam($name,$default=NULL){
	if(array_key_exists($name, $_REQUEST)){
		return $_REQUEST[$name];
	}
	return $default;
}
/**
 * The default action - show the home page
 */
function indexAction ()
{
	$file = getParam('file',NULL);
	$output = getParam('output',NULL);
	$folder = getParam('folder',NULL);
	$classname = getParam('classname',NULL);
	$definitions = array();
	if($file && file_exists($file)){
		$parser = new CZ_Tools_WSDLParser($file);
		$types = $parser->getTypeList();
		$methods = $parser->getMethodList();
		$definitions[$file] = array('types'=>$types, 'methods'=>$methods);
	}else if($folder && file_exists($folder)){
		$d = dir($folder);
		while(FALSE !== ($entry = $d->read())){
			$path = sprintf("%s%s%s", $folder, DIRECTORY_SEPARATOR, $entry);
			if(FALSE === is_file($path)){
				continue;
			}
			$pathinfo = pathinfo($path);
			$extension = $pathinfo['extension'];
			if($extension == 'wsdl' || $extension == 'xml'){
				$parser = new CZ_Tools_WSDLParser($path);
				$types = $parser->getTypeList();
				$methods = $parser->getMethodList();
				$definitions[$path] = array('types'=>$types, 'methods'=>$methods);
			}
		}
		$d->close();
	}
	performOutput($definitions, $output, $classname);
}

function performOutput($definitions, $output, $classname=NULL){
	global $generatedString;
	$modelMapping = array();
	$classMap = array();
	$outputToFolder = FALSE;
	$modelFolder = "";
	$serviceFolder = "";
	if(NULL == $classname){
		$classname = "ServiceWrapper";
	}
	if($output && FALSE == is_file($output)){
		$outputToFolder = TRUE;
		$modelFolder = sprintf("%s%sModel", $output, DIRECTORY_SEPARATOR);
		$serviceFolder = sprintf("%s%sSvr", $output, DIRECTORY_SEPARATOR);
		@exec(sprintf('mkdir -p %s', $modelFolder));
		@chmod(sprintf('chmod 777 %s', $modelFolder));
		@exec(sprintf('mkdir -p %s', $serviceFolder));
		@chmod(sprintf('chmod 777 %s', $serviceFolder));
		if(file_exists($modelFolder) == FALSE || file_exists($serviceFolder) == FALSE){
			printf("Failed to create folder for model or service\n");
			exit(1);
		}
	}
	foreach($definitions as $file=>$def){
		$types = $def['types'];
		$methods = $def['methods'];
		foreach ($types as $index=>$type){
			$old = $type['name'];
			$new = $old;
			$new = sprintf("Model_%s", $new);
			if(!array_key_exists($old, $modelMapping)){
				$modelMapping[$old] = $new;
			}
			if(!array_key_exists($file, $classMap)){
				$classMap[$file][$old] = $new;
			}
		}
	}
	foreach($definitions as $file=>$def){
		$types = $def['types'];
		$methods = $def['methods'];
		//getting module parts from file name
		$currentClassMap = $classMap[$file];
		CZ_Tools_WSDLParser::applyModeNameChange($modelMapping,$types,'model');
		CZ_Tools_WSDLParser::applyModeNameChange($modelMapping,$methods,'method');
		//Write data models
		foreach($types as $type){
			$model = CZ_Tools_WSDLParser::generateCodeForType($type);
			if($outputToFolder){
				$short = $type['name'];
				$parts = explode('_', $short);
				$filename = sprintf("%s%s%s.php", $modelFolder, DIRECTORY_SEPARATOR,$parts[count($parts)-1]);
				$f = fopen($filename, 'w');
				if($f === FALSE){
					printf("Failed to create file %s\n", $filename);
					exit(1);
				}
				fwrite($f, $model);
				fclose($f);
				@exec('chmod 666 '. $filename);
			}else{
				$generatedString = sprintf("%s\n%s", $generatedString, $model);
			}
		}
		//Write helper classes
		$class = CZ_Tools_WSDLParser::generateCodeForMethods($methods, 
			sprintf('%s', $classname),$currentClassMap,'SoapClient');
		if($outputToFolder){
			//write output to file
			$folder = $serviceFolder.DIRECTORY_SEPARATOR;
			$filename = sprintf("%s%s.php", $folder, $classname);
			$f = fopen($filename, 'w');
			if($f === FALSE){
				printf("Failed to create file %s\n", $filename);
				exit(1);
			}
			fwrite($f, $class);
			fclose($f);
			@exec('chmod 666 '. $filename);
		}else{
			$generatedString = sprintf("%s\n%s", $generatedString, $class);
		}
	}
}
indexAction();
?>
<html>
<head>
<title>Extractor for extracting message type as model and methods as wrapper class in php</title>
</head>
<body>
<form action="" method="POST">
<label>WSDL Location:<label>&nbsp;<input type="input" <?php if($_REQUEST['file']){echo sprintf('value="%s"',$_REQUEST['file']);}?> name="file" style="width:320px;"><label>(could be file location or url)</label><br/><br/>
	<label>Output location:<label>&nbsp;<input type="input" name="output" style="width:320px;"><label>(could be a server side folder or nothing)</label><br/><br/>
	<input type="Submit" value="Submit"><br/>
<?php if($generatedString):?>
<textarea style="width:500px; height: 400px;">
<?php
	echo $generatedString;
?>
</textarea>
<?php endif;?>
</form>
</body>
</html>
